package com.hzcx.app.simplechat.ui.chat;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import indi.liyi.viewer.Utils;

public class GroupJoinSettingActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;

    @BindView(R.id.join_mode_any)
    TextView joinModeAny;
    @BindView(R.id.join_mode_need_question)
    TextView joinModeNeddQuestion;
    @BindView(R.id.join_mode_prohibit)
    TextView joinModeProhibit;
    @BindView(R.id.join_check_question)
    CheckedTextView joinCheckQuestion;
    @BindView(R.id.join_check_question_examine)
    CheckedTextView joinCheckQuestionExamine;
    @BindView(R.id.join_mode_question)
    EditText joinModeQuestion;

    @BindView(R.id.join_mode_question_title)
    TextView joinModeQuestionTitle;

//    public static String GroupInfoBeanKey = "ChatGroupManagerSettingActivityGroupInfoBean";
//    GroupInfoBean groupInfoBean;
    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;

    private int tag;
    private String originStr = " ";

    @Override
    public int getLayout() {
        return R.layout.activity_group_join_setting;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

        tvTitle.setText("加群方式");
        tvTitleRight.setText("完成");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(GroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
        tag = Integer.valueOf(groupInfoBean.getJoin_mode());


       if (groupInfoBean.getQuestion1() != null && !groupInfoBean.getQuestion1().isEmpty()) {
           originStr = groupInfoBean.getQuestion1();
        }
        joinModeQuestion.setText(originStr);

        changeQuestion(tag);
        changeIcon(tag);
    }

    @Override
    public void initData() {

    }


//    R.id.join_mode_question
    @OnClick({R.id.tv_title_right, R.id.join_mode_any, R.id.join_mode_need_question, R.id.join_mode_prohibit,
            R.id.join_check_question, R.id.join_check_question_examine})
    public void viewOnClick(View view) {
        AppHelper.closeSoftKeybord(view, GroupJoinSettingActivity.this);
        switch (view.getId()) {
            case R.id.join_mode_any:
                if (tag != 0) {
                    tag = 0;
                    changeIcon(tag);
                    changeQuestion(tag);
                }
                break;
            case R.id.join_mode_need_question:
                if (tag != 1 || tag != 2) {
                    tag = 1;
                    changeIcon(tag);
                    changeQuestion(tag);
                }
                break;
            case R.id.join_mode_prohibit:
                if (tag != 3) {
                    tag = 3;
                    changeIcon(tag);
                    changeQuestion(tag);
                }
                break;
            case R.id.join_check_question:
                tag = 1;
                joinCheckQuestion.setChecked(true);
                joinCheckQuestionExamine.setChecked(false);
                break;
            case R.id.join_check_question_examine:
                tag = 2;
                joinCheckQuestion.setChecked(false);
                joinCheckQuestionExamine.setChecked(true);
                break;
            case R.id.tv_title_right:
                if (tag == 1 || tag == 2) {
                    if (joinModeQuestion.getText().toString().isEmpty()) {
                        ToastUtils.show("请输入入群的问题");
                        return;
                    }
                    originStr = joinModeQuestion.getText().toString();
                }
                GroupModel.modifyJoinModeGroup(this, groupInfoBean.getGroup_id(), String.valueOf(tag), originStr, new BaseObserver<BaseEmptyBean>() {
                    @Override
                    protected void onSuccess(BaseEmptyBean data) {
                        GroupInfoBean groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
                        groupInfoBean.setJoin_mode(String.valueOf(tag));
                        groupInfoBean.setQuestion1(originStr);

                        if (tag == 0) {
                            groupInfoBean.setJoin_mode_text_zh_cn("允许任何人加群");
                        } else if (tag == 1) {
                            groupInfoBean.setJoin_mode_text_zh_cn("需要加群理由");
                        } else if (tag == 2) {
                            groupInfoBean.setJoin_mode_text_zh_cn("需要加群理由并由管理员审核");
                        } else if (tag == 3) {
                            groupInfoBean.setJoin_mode_text_zh_cn("不允许任何人加群");
                        }
                        ToastUtils.show("修改成功");
                        finish();
                    }
                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        super.onFailure(e, errorMsg);
                        ToastUtils.show("修改失败，请稍后重试");
                    }
                });
                break;
        }
    }


    private void changeQuestion(int tag) {
        if (tag == 1 || tag == 2) {
            joinCheckQuestion.setVisibility(View.VISIBLE);
            joinCheckQuestionExamine.setVisibility(View.VISIBLE);
            joinModeQuestion.setVisibility(View.VISIBLE);
            joinModeQuestionTitle.setVisibility(View.VISIBLE);
            if (tag == 1) {
                joinCheckQuestion.setChecked(true);
                joinCheckQuestionExamine.setChecked(false);
            } else {
                joinCheckQuestion.setChecked(false);
                joinCheckQuestionExamine.setChecked(true);
            }
        } else {
            joinCheckQuestion.setVisibility(View.GONE);
            joinCheckQuestionExamine.setVisibility(View.GONE);
            joinModeQuestion.setVisibility(View.GONE);
            joinModeQuestionTitle.setVisibility(View.GONE);
        }
    }

    private void changeIcon(int tag) {
        Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_icon_chat_read);
        drawable.setBounds(1, 1, 40, 30);
        if (tag == 0) {
            joinModeAny.setCompoundDrawables(null, null, drawable, null);
            joinModeNeddQuestion.setCompoundDrawables(null, null, null, null);
            joinModeProhibit.setCompoundDrawables(null, null, null, null);
        } else if (tag == 1 || tag == 2) {
            joinModeAny.setCompoundDrawables(null, null, null, null);
            joinModeNeddQuestion.setCompoundDrawables(null, null, drawable, null);
            joinModeProhibit.setCompoundDrawables(null, null, null, null);
        } else if (tag == 3) {
            joinModeAny.setCompoundDrawables(null, null, null, null);
            joinModeNeddQuestion.setCompoundDrawables(null, null, null, null);
            joinModeProhibit.setCompoundDrawables(null, null, drawable, null);
        }
    }
}
